package com.dkt.blogboot.util;

import com.dkt.blogboot.entity.Category;
import com.dkt.blogboot.entity.Tag;
import com.dkt.blogboot.entity.User;
import com.dkt.blogboot.mapper.CategoryMapper;
import com.dkt.blogboot.mapper.TagMapper;
import com.dkt.blogboot.mapper.UserMapper;
import net.minidev.json.JSONValue;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class RabbitMQConfig {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "blog.updateQueue"),
            exchange = @Exchange(name = "blog.direct",type = ExchangeTypes.DIRECT),
            key = {"blog.update"}
    ))
    public void UpdateTopicQueue(String msg) throws IOException {
        if (msg.equals("updated")){
            List<User> userToUpdate = userMapper.findUserToUpdate();
            System.out.println(userToUpdate.size()+"条数据要被同步");
            for (User user : userToUpdate) {
                System.out.println("这是"+user.getNickname());
                String dataToCopy = JSONValue.toJSONString(user);
                IndexRequest request = new IndexRequest("user_reg_es").id(user.getId() + "");
                request.source(dataToCopy, XContentType.JSON);
                IndexResponse resp = restHighLevelClient.index(request, RequestOptions.DEFAULT);
                request.source(dataToCopy, XContentType.JSON);
                if (resp.getResult() == DocWriteResponse.Result.CREATED||
                        resp.getResult() == DocWriteResponse.Result.UPDATED){
                    System.out.println("ID为"+user.getId()+"的数据已同步到es中！");
                    userMapper.updateUserStatus(user.getId(),0);
                }
            }
        }
        System.out.println("声明能接收MQ增加或修改信息的队列，并绑定交换机");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "blog.deleteQueue"),
            exchange = @Exchange(name = "blog.direct",type = ExchangeTypes.DIRECT),
            key = {"blog.delete"}
    ))
    public void DeleteTopicQueue(Integer msg) throws IOException {
        DeleteRequest req = new DeleteRequest("user_reg_es", msg + "");
        DeleteResponse resp = restHighLevelClient.delete(req, RequestOptions.DEFAULT);
        if (resp.getResult() == DocWriteResponse.Result.DELETED){
            System.out.println("ID为"+msg+"的数据已同步从es中删除！");
        }else {
            System.out.println("ID为"+msg+"的数据删除未成功！若无报错可能是ES中无此数据");
        }
        System.out.println("声明能接收M删除信息的队列，并绑定交换机");
    }

}
